<?php

function fetch_video_url($url)
{	

	$source_url = "";
	if(strpos($url,'www.tudou.com')!==false)
	{
		if(strpos($url,'.html'))
		{
            $files=file_get_url($url);
			preg_match("/iid:(\s+)(\w+)/i",$files,$iid);
            preg_match('/lcode:(\s+)\'(\w+)\'/is',$files,$lcode);
			if(!empty($iid[1]) && !empty($lcode[1]))
			{
				$source_url= sprintf('http://www.tudou.com/a/%s/&iid=%s/v.swf',$lcode[1],$iid[1]);
			}else{
                $source_url = $url;
            }
			
		}
		else
		{
            $files=file_get_url($url);
			preg_match("/iid\s*|\s*=\s*|\s*(\w+)/i",$files,$items);
			$iid=$items[1];		
			if(empty($iid))
			{
				$source_url=tudou_get($url);
			}else{
                $source_url = $url;
            }
		}
	
	
	}
	elseif(strpos($url,'v.ku6.com')!==false)
	{
        preg_match("/show\/(.*)\.html/is",$url,$items);
        if(isset($items[1])){
            $source_url = sprintf('http://player.ku6.com/refer/%s/v.swf',$items[1]);
        }else{
            $source_url = $url;
        }
	}
	elseif(strpos($url,'v.youku.com')!==false)
	{
		preg_match("/id_(.*)\.html/is",$url,$items);
        if(isset($items[1])){
            $source_url = sprintf('http://player.youku.com/player.php/sid/%s/v.swf',$items[1]);
        }else{
            $source_url = $url;
        }
	}else{
        $source_url = $url;
    }
	return $source_url;
}


function file_get_url($url=''){
	if(!empty($url)){
		$ctx = stream_context_create(array(
			'method'=>'GET',
			'http' => array(  
				'timeout' => 1 //设置一个超时时间，单位为秒  
			)  
		)  
		); 
		$files=file_get_contents($url,false,$ctx);
		return $files;
	}else{
		return false;
	}
}

function tudou_get($url){
	preg_match('#https?://(?:www\.)?tudou\.com/(?:programs/view|listplay/(?<list_id>[a-z0-9_=\-]+))/(?<video_id>[a-z0-9_=\-]+)#i', $url, $matches );
	return  handler_tudou( $matches);
}

function handler_tudou( $matches ) {
	
  $embed = sprintf(
    'http://www.tudou.com/v/%1$s/&resourceId=0_05_05_99&bid=05/v.swf',
    $matches['video_id'] );

  return $embed;
}

?>